{% extends "navigationbar.html" %}
{% load static %}
{% block title %}心灵港湾 - 个人中心{% endblock %}

{% block css %}
    <style>
        /* 新增导航栏高度对应的顶部边距 */
        .container {
            margin-top: 80px; /* 根据实际导航栏高度调整这个值 */
            padding: 20px;
        }

        .header {
            height: 80px; /* 如果尚未定义请添加这一行 */
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 1000; /* 保持导航栏在最上层 */
        }

        :root {
            --primary-color: #FFB6C1;
            --secondary-color: #FFDAB9;
            --accent-color: #FF69B4;
            --bg-color: #FFF5F5;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            background: var(--bg-color);
            line-height: 1.6;
            color: #666;
        }

        /* 头部区域 */
        .profile-header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            padding: 2rem 1rem;
            text-align: center;
            border-radius: 0 0 30px 30px;
            box-shadow: 0 4px 15px rgba(255, 182, 193, 0.2);
            position: relative;
        }

        /* 头像上传 */
        .avatar-upload {
            position: relative;
            display: inline-block;
            cursor: pointer;
            margin-bottom: 1rem;
        }

        .avatar {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            border: 4px solid white;
            object-fit: cover;
            transition: transform 0.3s;
        }

        .upload-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            border-radius: 50%;
            opacity: 0;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }

        .avatar-upload:hover .upload-overlay {
            opacity: 1;
        }

        /* MBTI徽章 */
        .mbti-badge {
            display: inline-flex;
            align-items: center;
            background: rgba(255, 255, 255, 0.2);
            color: white;
            padding: 0.3rem 1.2rem;
            border-radius: 20px;
            margin-top: 0.8rem;
            font-size: 0.95em;
            border: 2px solid white;
            gap: 0.5rem;
            transition: all 0.3s;
        }

        .mbti-badge:hover {
            transform: scale(1.05);
            background: rgba(255, 255, 255, 0.3);
        }

        /* 个人信息卡片 */
        .info-card {
            background: white;
            padding: 3rem 1.5rem 1.5rem;
            margin: 2rem auto;
            max-width: 800px;
            border-radius: 15px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            position: relative;
        }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 1.5rem;
            margin-top: 1rem;
        }

        .info-item {
            display: flex;
            align-items: center;
            padding: 1rem;
            background: #fff0f5;
            border-radius: 10px;
            position: relative;
        }

        .info-item i {
            width: 30px;
            color: var(--accent-color);
            margin-right: 1rem;
        }

        /* 编辑模式 */
        .edit-mode .info-text {
            display: none;
        }

        .edit-mode .edit-inputs {
            display: block;
        }

        .edit-btns {
            display: none;
            justify-content: flex-end;
            gap: 1rem;
            margin-top: 1.5rem;
        }

        .edit-mode .edit-btns {
            display: flex;
        }

        .edit-inputs {
            display: none;
            width: 100%;
        }

        select, input {
            width: 100%;
            padding: 0.5rem;
            border: 2px solid var(--primary-color);
            border-radius: 8px;
            background: #fff;
        }

        /* 按钮样式 */
        .edit-btn {
            position: absolute;
            top: 20px;
            left: 20px;
            background: rgba(255, 255, 255, 0.9);
            color: var(--accent-color);
            padding: 0.5rem 1.5rem;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .contact-btn {
            background: var(--accent-color);
            color: white;
            padding: 0.8rem 1.5rem;
            border-radius: 25px;
            text-decoration: none;
            transition: all 0.3s;
            border: none;
            cursor: pointer;
        }

        button:hover, .contact-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(255, 105, 180, 0.3);
        }

        /* 成功提示 */
        .success-toast {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: #8BC34A;
            color: white;
            padding: 1rem 2rem;
            border-radius: 30px;
            animation: slideIn 0.5s, fadeOut 1s 2s forwards;
            box-shadow: 0 4px 15px rgba(139, 195, 74, 0.3);
            display: none;
            z-index: 1000;
        }

        @keyframes slideIn {
            from {
                top: -50px;
            }
            to {
                top: 20px;
            }
        }

        @keyframes fadeOut {
            from {
                opacity: 1;
            }
            to {
                opacity: 0;
            }
        }

        /* 退出按钮 */
        .logout-btn {
            position: absolute;
            top: 20px;
            right: 20px;
            color: white;
            text-decoration: none;
            padding: 0.5rem 1rem;
            border-radius: 20px;
            background: rgba(255, 255, 255, 0.2);
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .info-grid {
                grid-template-columns: 1fr;
            }

            .edit-btn, .logout-btn {
                position: static;
                margin: 0.5rem;
            }

            .avatar {
                width: 100px;
                height: 100px;
            }
        }
    </style>
{% endblock %}

{% block body %}
    <div class="profile-header">

        <div class="avatar-upload">
            <img src="{% static 'img/userAvatar_default.png' %}" alt="头像" class="avatar" id="avatarImg">
            <input type="file" id="avatarInput" accept="image/*">
        </div>

        <h1 style="color: white; margin: 0.5rem 0;">
            <span id="nameText" class="name-display">{{ username }}</span>
        </h1>
        <div class="mbti-badge">
            <i class="fas fa-brain"></i>
            <span id="mbtiHeaderText">{{ mbti }}</span>
        </div>
        <p style="color: rgba(255,255,255,0.9); margin-top: 0.5rem;">心理咨询师</p>
    </div>

    <div class="info-card">
        <form action="/personalcenter/" class="login-wrapper" method="post" novalidate>
            {% csrf_token %}
            <div class="edit-btn" onclick="toggleEdit()">
                <i class="fas fa-edit"></i> 编辑资料
            </div>

            <div class="info-grid">
                <div class="info-item" style="grid-column: 1 / -1;">
                    <div class="info-text">
                        <i class="fas fa-user-edit"></i>
                        <div>
                            <h3>姓名</h3>
                            <p id="nameDisplayText">{{ username }}</p>
                        </div>
                    </div>
                    <div class="edit-inputs">
                        {{ InfoForm.first_name }}
                        <span style="color: red">{{ InfoForm.first_name.errors.0 }}</span>
                    </div>
                    <div class="edit-inputs">
                        {{ InfoForm.last_name }}
                        <span style="color: red">{{ InfoForm.last_name.errors.0 }}</span>
                    </div>

                </div>

                <div class="info-item">
                    <div class="info-text">
                        <i class="fas fa-venus"></i>
                        <div>
                            <h3>性别</h3>
                            <p id="genderText">{{ gender }}</p>
                        </div>
                    </div>
                    <div class="edit-inputs">
                        {{ InfoForm.gender }}
                        <span style="color: red">{{ InfoForm.gender.errors.0 }}</span>
                    </div>
                </div>

                <div class="info-item">
                    <div class="info-text">
                        <i class="fas fa-birthday-cake"></i>
                        <div>
                            <h3>出生年月 | 星座</h3>
                            <p id="birthText">{{ birthday }} | 双鱼座</p>
                        </div>
                    </div>
                    <div class="edit-inputs">
                        {{ InfoForm.birthday }}
                        <span style="color: red">{{ InfoForm.birthday.errors.0 }}</span>
                        <small style="color:#999">修改日期后星座会自动更新</small>
                    </div>
                </div>

                <div class="info-item">
                    <div class="info-text">
                        <i class="fas fa-brain"></i>
                        <div>
                            <h3>MBTI人格</h3>
                            <p id="mbtiText">{{ mbti }}</p>
                        </div>
                    </div>
                    <div class="edit-inputs">
                        {{ InfoForm.mbti }}
                        <span style="color: red">{{ InfoForm.mbti.errors.0 }}</span>
                    </div>
                </div>

                <div class="info-item">
                    <div class="info-text">
                        <i class="fas fa-phone"></i>
                        <div>
                            <h3>联系电话</h3>
                            <p id="phoneText">{{ phone }}</p>
                        </div>
                    </div>
                    <div class="edit-inputs">
                        {{ InfoForm.phone }}
                        <span style="color: red">{{ InfoForm.phone.errors.0 }}</span>
                    </div>
                </div>
            </div>

            <div class="edit-btns">
                <button type="submit" class="contact-btn">
                    <i class="fas fa-save"></i> 保存修改
                </button>
                <button type="reset" class="contact-btn" style="background:#ccc" onclick="toggleEdit()">
                    <i class="fas fa-times"></i> 取消
                </button>
            </div>
        </form>
    </div>

    <div class="success-toast" id="successToast">
        <i class="fas fa-check-circle"></i> 修改保存成功！
    </div>
{% endblock %}

{% block script %}
<script>
    // 星座计算函数
    function getZodiac(birthday) {
        const date = new Date(birthday);
        const month = date.getMonth() + 1;
        const day = date.getDate();

        const zodiacs = [
            [3, 21, "白羊座"], [4, 20, "金牛座"], [5, 21, "双子座"],
            [6, 22, "巨蟹座"], [7, 23, "狮子座"], [8, 23, "处女座"],
            [9, 23, "天秤座"], [10, 23, "天蝎座"], [11, 22, "射手座"],
            [12, 22, "摩羯座"], [1, 20, "水瓶座"], [2, 19, "双鱼座"]
        ];

        for (let [m, d, sign] of zodiacs) {
            if ((month === m && day >= d) ||
                (month === m % 12 + 1 && day < d)) {
                return sign;
            }
        }
        return "未知星座";
    }

    // 编辑模式切换
    let isEditing = false;

    function toggleEdit() {
        const card = document.querySelector('.info-card');
        isEditing = !isEditing;
        card.classList.toggle('edit-mode');
    }

    // 自动更新星座
    document.getElementById('birthInput').addEventListener('change', function () {
        const zodiac = getZodiac(this.value);
        document.getElementById('birthText').innerHTML =
            `${this.value} | ${zodiac}`;
    });

    // 头像上传
    document.getElementById('avatarInput').addEventListener('change', function (e) {
        const file = e.target.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function (e) {
                document.getElementById('avatarImg').src = e.target.result;
                showSuccessToast();
            }
            reader.readAsDataURL(file);
        }
    });

    // 保存修改
    function saveChanges() {
        const newName = document.getElementById('nameInput').value.trim();

        // 验证输入
        if (newName === '') return alert('姓名不能为空');
        if (newName.length > 20) return alert('姓名不能超过20个字符');

        // 更新显示内容
        document.getElementById('nameText').textContent = newName;
        document.getElementById('nameDisplayText').textContent = newName;
        document.getElementById('genderText').textContent =
            document.getElementById('genderInput').value;

        const newMbti = document.getElementById('mbtiInput').value.toUpperCase();
        document.getElementById('mbtiText').textContent = newMbti;
        document.getElementById('mbtiHeaderText').textContent = newMbti;

        document.getElementById('phoneText').textContent =
            document.getElementById('phoneInput').value;

        showSuccessToast();
        toggleEdit();
    }

    // 显示成功提示
    function showSuccessToast() {
        const toast = document.getElementById('successToast');
        toast.style.display = 'block';
        setTimeout(() => {
            toast.style.display = 'none';
        }, 2500);
    }

    // 拖放上传功能
    const avatarArea = document.querySelector('.avatar-upload');
    avatarArea.addEventListener('dragover', e => {
        e.preventDefault();
        avatarArea.style.transform = 'scale(1.05)';
    });

    avatarArea.addEventListener('dragleave', e => {
        avatarArea.style.transform = 'scale(1)';
    });

    avatarArea.addEventListener('drop', e => {
        e.preventDefault();
        avatarArea.style.transform = 'scale(1)';
        const file = e.dataTransfer.files[0];
        if (file && file.type.startsWith('image/')) {
            document.getElementById('avatarInput').files = e.dataTransfer.files;
            document.getElementById('avatarInput').dispatchEvent(new Event('change'));
        }
    });

    // 点击姓名进入编辑模式
    document.getElementById('nameText').addEventListener('click', () => {
        if (!isEditing) toggleEdit();
    });
</script>
{% endblock %}

